package com.example.studyktdemo.ui.fragment

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.studyktdemo.R
import com.example.studyktdemo.adapter.DiscoveryPageAdapter
import com.example.studyktdemo.base.BaseFragment
import com.example.studyktdemo.databinding.FragmentDiscoveryBinding
import com.example.studyktdemo.uitls.ResourceUtils
import com.example.studyktdemo.viewmodel.DiscoveryViewModel
import com.google.android.material.tabs.TabLayoutMediator


/**
 * A simple [Fragment] subclass.
 * Use the [DiscoveryFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class DiscoveryFragment : BaseFragment<FragmentDiscoveryBinding>() {

    private var tabList = ArrayList<String>()
    private var fragments = ArrayList<Fragment>()

    private var adapter:DiscoveryPageAdapter? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initPager()
    }

    //结合tablayout和viewpager
    private fun initPager() {
        adapter = fragmentManager?.let { DiscoveryPageAdapter(it,lifecycle,fragments) }
        binding.discoveryContent.adapter = adapter

        TabLayoutMediator(binding.discoveryTab,binding.discoveryContent) { tab, position ->
            //binding.discoveryTab.selectTab(tab)
            tab.text = tabList[position]
        }.attach()
    }

    override fun initView() {
        tabList.add(ResourceUtils.getString(R.string.attention))
        tabList.add(ResourceUtils.getString(R.string.category))
    }

    override fun initData() {
        fragments.add(FocusOnFragment.getInstance())
        fragments.add(CategoryFragment.getInstance())
    }

    override fun bindLayout(): Int {
        return R.layout.fragment_discovery
    }

    override fun initObserver() {

    }

    override fun initListener() {

    }
}